﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;

namespace SharpZipLibAndStream
{
    class Program
    {
        static void Main(string[] args)
        {
            MemoryStream memoryStream = new MemoryStream();
            FillStream(memoryStream,100);
            Console.WriteLine(string.Format("Изначальная длинна потока: {0} bytes", memoryStream.Length));

            MemoryStream zippedMemoryStream = new MemoryStream();
            ZipOutputStream zipStream = new ZipOutputStream(zippedMemoryStream);
            ZipEntry zipEntry = new ZipEntry("myData");
            zipStream.PutNextEntry(zipEntry);
            FillStream(zipStream,100);                       
            zipStream.CloseEntry();
            Console.WriteLine(string.Format("Длинна потока в архиве: {0} bytes", zippedMemoryStream.Length));
                        
            ZipInputStream zipInputStream = new ZipInputStream(zippedMemoryStream);            
            zippedMemoryStream.Position = 0;
            MemoryStream fromZippedMemoryStream = new MemoryStream();
            ZipEntry entry = zipInputStream.GetNextEntry();
            byte[] outputBuffer = new byte[zipInputStream.Length];
            zipInputStream.Read(outputBuffer, 0, outputBuffer.Length);
            fromZippedMemoryStream.Write(outputBuffer, 0, outputBuffer.Length);
            Console.WriteLine(string.Format("Длинна разархивированного потока: {0} bytes", fromZippedMemoryStream.Length));
        }

        private static void FillStream(Stream stream, int times)
        {
            string temp = "The quick brown fox jumps over the lazy dog";
            string data = string.Empty;
            for (int i = 0; i < times; i++) data += temp;
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(data);
            stream.Write(buffer, 0, buffer.Length);
        }
    }
}
